package com.base.study.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.base.commonlib.utils.RxBus2;
import com.base.study.R;
import com.base.study.bean.CurriculumCatalogBean;

import java.util.List;

/**
 * Created by zw on 2020/2/21.
 */

public class CurriculumCatalogAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<CurriculumCatalogBean> curriculumCatalogBeans;
    private final LayoutInflater mInflater;
    public CurriculumCatalogAdapter(Context context, List<CurriculumCatalogBean> curriculumCatalogBeans){
        this.mContext = context;
        this.curriculumCatalogBeans = curriculumCatalogBeans;
        mInflater = LayoutInflater.from(context);
    }


    //父项的个数
    @Override
    public int getGroupCount() {
        return curriculumCatalogBeans.size();
    }
    //某个父项的子项的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return curriculumCatalogBeans.get(groupPosition).getWares().size();
    }
    //获得某个父项
    @Override
    public Object getGroup(int groupPosition) {
        return curriculumCatalogBeans.get(groupPosition);
    }
    //获得某个子项
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return curriculumCatalogBeans.get(groupPosition).getWares();
    }
    //父项的Id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    //子项的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    //获取父项的view
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.item_curriculum_group,parent,false);
        }
        String group = curriculumCatalogBeans.get(groupPosition).getName();
        TextView tvGroup = (TextView) convertView.findViewById(R.id.group_title);
        tvGroup.setText(group);
        return convertView;
    }
    //获取子项的view
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String child = curriculumCatalogBeans.get(groupPosition).getWares().get(childPosition).getName();
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.item_curriculum_catalog,parent,false);
        }
        TextView tvChild = (TextView)convertView.findViewById(R.id.name);
        tvChild.setText(child);
        return convertView;
    }
    //子项是否可选中,如果要设置子项的点击事件,需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
